package com.caojin.propertyInjector.spring;

import java.lang.annotation.Annotation;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;

import com.caojin.propertyInjector.annotation.AnnotationResolver;

/**
 * spring mvc中将配置信息注入到方法的参数上
 * @author caojin
 */
public class PropertyArgumentResolver implements WebArgumentResolver {

	//注解解析器
	private AnnotationResolver annotationResolver;

	@Override
	public Object resolveArgument(MethodParameter methodParameter,
			NativeWebRequest webRequest) throws Exception {
		Annotation[] annotaions = methodParameter.getParameterAnnotations();
		for (Annotation annotation : annotaions) {
			String value = annotationResolver.getInjectValue(annotation);
			if (value == AnnotationResolver.UNRESOLVE_ANNOTATION) {
				return WebArgumentResolver.UNRESOLVED;
			}
			return value;
		}
		return WebArgumentResolver.UNRESOLVED;
	}

	public void setAnnotationResolver(AnnotationResolver annotationResolver) {
		this.annotationResolver = annotationResolver;
	}
}
